<?php

/**
 * Narudzbina form base class.
 *
 * @package    marketing
 * @subpackage form
 * @author     Your name here
 */
class BaseNarudzbinaForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                      => new sfWidgetFormInputHidden(),
      'datum_narudzbine'        => new sfWidgetFormDateTime(),
      'status_id'               => new sfWidgetFormPropelChoice(array('model' => 'Status', 'add_empty' => false)),
      'kupac_id'                => new sfWidgetFormPropelChoice(array('model' => 'Kupac', 'add_empty' => false)),
      'nacin_placanja_id'       => new sfWidgetFormPropelChoice(array('model' => 'NacinPlacanja', 'add_empty' => false)),
      'nacin_isporuke_id'       => new sfWidgetFormPropelChoice(array('model' => 'NacinIsporuke', 'add_empty' => false)),
      'created_at'              => new sfWidgetFormDateTime(),
      'updated_at'              => new sfWidgetFormDateTime(),
      'deleted_at'              => new sfWidgetFormDateTime(),
      'narudzbina_prozvod_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Proizvod')),
    ));

    $this->setValidators(array(
      'id'                      => new sfValidatorPropelChoice(array('model' => 'Narudzbina', 'column' => 'id', 'required' => false)),
      'datum_narudzbine'        => new sfValidatorDateTime(),
      'status_id'               => new sfValidatorPropelChoice(array('model' => 'Status', 'column' => 'id')),
      'kupac_id'                => new sfValidatorPropelChoice(array('model' => 'Kupac', 'column' => 'id')),
      'nacin_placanja_id'       => new sfValidatorPropelChoice(array('model' => 'NacinPlacanja', 'column' => 'id')),
      'nacin_isporuke_id'       => new sfValidatorPropelChoice(array('model' => 'NacinIsporuke', 'column' => 'id')),
      'created_at'              => new sfValidatorDateTime(array('required' => false)),
      'updated_at'              => new sfValidatorDateTime(array('required' => false)),
      'deleted_at'              => new sfValidatorDateTime(array('required' => false)),
      'narudzbina_prozvod_list' => new sfValidatorPropelChoiceMany(array('model' => 'Proizvod', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('narudzbina[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Narudzbina';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['narudzbina_prozvod_list']))
    {
      $values = array();
      foreach ($this->object->getNarudzbinaProzvods() as $obj)
      {
        $values[] = $obj->getProizvodId();
      }

      $this->setDefault('narudzbina_prozvod_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveNarudzbinaProzvodList($con);
  }

  public function saveNarudzbinaProzvodList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['narudzbina_prozvod_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(NarudzbinaProzvodPeer::NARUDZBINA_ID, $this->object->getPrimaryKey());
    NarudzbinaProzvodPeer::doDelete($c, $con);

    $values = $this->getValue('narudzbina_prozvod_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new NarudzbinaProzvod();
        $obj->setNarudzbinaId($this->object->getPrimaryKey());
        $obj->setProizvodId($value);
        $obj->save();
      }
    }
  }

}
